package com.csdj.exception;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: GlobalExceptionHandler
 * @Author: ZouLi
 * @Description:
 * @Date: 2022/9/13 14:15
 **/
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    public String exceptionHandler(Exception e, Model model){
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("code",300);
        map.put("msg","操作失败"+e.getMessage());
        if(e instanceof BusinessException){
            BusinessException bu= (BusinessException) e;
            map.put("code",bu.getCode());
            map.put("msg",bu.getMsg());
        }
        model.addAttribute("message",map);
        return "error";
    }
}
